/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ysw.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ysw.dao.RechargeDao;
import ysw.databaseconnection.DBConn;
import ysw.vo.Recharge;


public class RechargeJdbc implements RechargeDao{
    private  Connection conn=null;
    private PreparedStatement psmt=null;
    private ResultSet rs=null;
    public RechargeJdbc(){}
    public Connection getConn(){
        try{
            if(this.conn==null||this.conn.isClosed()){
                DBConn mc=new DBConn();
                this.conn=mc.getConn();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }
     public Recharge addRecharge(Recharge recharge){
      String sql="insert into recharges values(?,?)";
        try{
            psmt=this.getConn().prepareStatement(sql);
    
            psmt.setInt(1,recharge.getId());
         
            psmt.setString(2,recharge.getDescribe());
          
         
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return recharge;   
     }
    public List showRecharge() throws Exception{
         String sql="select * from recharges";
    List rechargeList=new ArrayList();
    try{
        psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
          Recharge recharge=new Recharge();
          recharge.setId(rs.getInt("id"));                 
          recharge.setDescribe(rs.getString("describe"));
   
        }
        return rechargeList;
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
             if(rs!=null){
                   rs.close();
                   rs=null;
                }
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return rechargeList;
    }
}
